package com.ruoyi.business.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Grade;
import com.ruoyi.business.service.IGradeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 年级Controller
 * 
 * @author lcz
 * @date 2025-03-31
 */
@Api(tags = "年级相关接口")
@RestController
@RequestMapping("/business/grade")
public class GradeController extends BaseController
{
    @Autowired
    private IGradeService gradeService;

    /**
     * 查询年级列表
     */
    @ApiOperation(value = "后台----查询年级列表")
//    @PreAuthorize("@ss.hasPermi('business:grade:list')")
    @GetMapping("/list")
    public TableDataInfo list(Grade grade)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (!ObjectUtils.isEmpty(sysUser.getSchoolId())){
            grade.setSchoolId(sysUser.getSchoolId());
        }
        startPage();
        List<Grade> list = gradeService.selectGradeList(grade);
        return getDataTable(list);
    }

    /**
     * 导出年级列表
     */
    @ApiOperation(value = "后台----导出年级列表")
//    @PreAuthorize("@ss.hasPermi('business:grade:export')")
    @Log(title = "年级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Grade grade)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (!ObjectUtils.isEmpty(sysUser.getSchoolId())){
            grade.setSchoolId(sysUser.getSchoolId());
        }
        List<Grade> list = gradeService.selectGradeList(grade);
        ExcelUtil<Grade> util = new ExcelUtil<Grade>(Grade.class);
        util.exportExcel(response, list, "年级数据");
    }

    /**
     * 获取年级详细信息
     */
    @ApiOperation(value = "后台----获取年级详细信息")
//    @PreAuthorize("@ss.hasPermi('business:grade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gradeService.selectGradeById(id));
    }

    /**
     * 新增年级
     */
    @ApiOperation(value = "后台----新增年级")
//    @PreAuthorize("@ss.hasPermi('business:grade:add')")
    @Log(title = "年级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Grade grade)
    {
        return toAjax(gradeService.insertGrade(grade));
    }

    /**
     * 修改年级
     */
    @ApiOperation(value = "后台----修改年级")
//    @PreAuthorize("@ss.hasPermi('business:grade:edit')")
    @Log(title = "年级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Grade grade)
    {
        return toAjax(gradeService.updateGrade(grade));
    }

    /**
     * 删除年级
     */
    @ApiOperation(value = "后台----删除年级")
//    @PreAuthorize("@ss.hasPermi('business:grade:remove')")
    @Log(title = "年级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gradeService.deleteGradeByIds(ids));
    }

    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/

    /**
     * 查询年级列表
     * 接口路径：/grade/front/list
     */
    @GetMapping("/front/list")
    @ApiOperation("前台查询年级列表")
    public TableDataInfo<Grade> frontList(Grade grade)
    {
        startPage();
        List<Grade> list = gradeService.selectGradeList(grade);
        return getDataTable(list);
    }

//    /**
//     * 获取年级详细信息
//     * 接口路径：/grade/front/info
//     */
//    @ApiOperation("前台获取年级详细信息")
//    @GetMapping(value = "/front/info")
//    public R<Grade> frontInfo(@RequestParam("id") Long id)
//    {
//        return R.ok(gradeService.selectGradeById(id));
//    }
//
//    /**
//     * 新增年级
//     * 接口路径：/grade/front/add
//     */
//    @ApiOperation("前台新增年级")
//    @PostMapping("/front/add")
//    public AjaxResult frontAdd(@RequestBody Grade grade)
//    {
//        return toAjax(gradeService.insertGrade(grade));
//    }
//
//    /**
//     * 修改年级
//     * 接口路径：/grade/front/edit
//     */
//    @ApiOperation("前台修改年级")
//    @PostMapping("/front/edit")
//    public AjaxResult frontEdit(@RequestBody Grade grade)
//    {
//        return toAjax(gradeService.updateGrade(grade));
//    }
//
//    /**
//     * 删除年级
//     * 接口路径：/grade/front/delete
//     */
//    @ApiOperation("前台删除年级")
//    @GetMapping("/front/delete")
//    public AjaxResult frontRemove(@RequestParam Long[] ids)
//    {
//        return toAjax(gradeService.deleteGradeByIds(ids));
//    }
}
